import java.util.Arrays;

public class project {
    public int maxProduct(int[] nums) {
        int len=nums.length;
        int ret=Integer.MIN_VALUE;
        int max=1;
        int min=1;
        for(int i=0;i<len;i++){
            if(nums[i]<0){
                int tmp=max;
                max=min;
                min=tmp;
            }
            max=Math.max(nums[i],max*nums[i]);
            min=Math.min(nums[i],min*nums[i]);
            ret=Math.max(ret,max);
        }
        return ret;
    }















    public int firstMissingPositive(int[] nums) {
        int len=nums.length;
        for(int i=0;i<len;i++){
            while (nums[i]>0&&nums[i]<=len&&nums[nums[i]-1]!=nums[i]){
                swap(nums,i,nums[i]-1);
            }
        }
        for(int i=0;i<len;i++){
            if(nums[i]!=i+1){
                return i+1;
            }
        }
        return len+1;
    }

    private void swap(int[] nums, int x, int y) {
        int tmp=nums[x];
        nums[x]=nums[y];
        nums[y]=tmp;
    }











    public int minMoves2(int[] nums) {
        Arrays.sort(nums);
        int len=nums.length;
        int mid=nums[len/2];
        int ret=0;
        for(int value:nums){
            ret+=Math.abs(mid-value);
        }
        return ret;
    }
}
